﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint;
using TST.DemoCode.EventReceivers;

namespace TST.DemoCode.Pages
{
    public class FoldersToFieldsSetup : LayoutsPageBase
    {
        protected DropDownList selectContentType;
        protected TextBox editFields;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            selectContentType.SelectedIndexChanged +=new EventHandler(selectContentType_SelectedIndexChanged);
            if (!Page.IsPostBack)
            {
                selectContentType.Items.Add(new ListItem("--- Selecteer een inhoudstype ---", "-"));
                foreach (SPContentType contentType in SPContext.Current.Web.ContentTypes)
                {
                    if (FoldersToFields.SupportsFoldersToFields(contentType))
                    {
                        selectContentType.Items.Add(new ListItem(contentType.Name, contentType.Id.ToString()));
                    }
                }
            }
        }

        protected void selectContentType_SelectedIndexChanged(object sender, EventArgs e)
        {
            editFields.Text = String.Empty;
            String key = SetupKey;
            if (String.IsNullOrEmpty(key))
            {
                return;
            }
            if (SPContext.Current.Web.AllProperties.ContainsKey(key))
            {
                if (SPContext.Current.Web.AllProperties[key] != null && !String.IsNullOrEmpty(SPContext.Current.Web.AllProperties[key].ToString()))
                {
                    String[] names = SPContext.Current.Web.AllProperties[key].ToString().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < names.Length; i++)
                    {
                        editFields.Text += String.Format("{0}{1}", names[i], Environment.NewLine);
                    }
                }
            }
        }

        private String SetupKey
        {
            get
            {
                if (selectContentType.SelectedValue == "-")
                {
                    return String.Empty;
                }
                SPContentTypeId newId = new SPContentTypeId(selectContentType.SelectedValue);
                SPContentType contentType = SPContext.Current.Web.ContentTypes[newId];
                return FoldersToFields.GetSetupPropertyKey(contentType);
            }
        }

        protected void buttonCancel_Click(object sender, EventArgs e)
        {
            SPUtility.Redirect("/_layouts/settings.aspx", SPRedirectFlags.Default, this.Context);
        }

        protected void buttonOK_Click(object sender, EventArgs e)
        {
            String key = SetupKey;
            if (!String.IsNullOrEmpty(key))
            {
                String[] names = editFields.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                StringBuilder value = new StringBuilder();
                for (int i = 0; i < names.Length; i++)
                {
                    value.AppendFormat("{0};", names[i]);
                }
                SPContext.Current.Web.Properties[key] = value.ToString();
                SPContext.Current.Web.AllProperties[key] = value.ToString();
                SPContext.Current.Web.Update();
                SPContext.Current.Web.Properties.Update();
            }
            SPUtility.Redirect("/_layouts/settings.aspx", SPRedirectFlags.Default, this.Context);
        }
    }
}
